declare var tt;
declare var wx;

import { _decorator, Component, find, Label, Node, ProgressBar } from 'cc';
import { ResMgr } from '../Script/Mgr/ResMgr';
import { AdMgr } from '../Script/Mgr/AdMgr';
import { AssetList, Global } from '../Script/Global';
import { UIMgr } from '../Script/Mgr/UIMgr';
import { LocalStorageMgr } from '../Script/Mgr/LocalStorageMgr';
import { AudioMgr, SoundName } from '../Script/Mgr/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('Loading')
/**
 * 加载界面
 */
export class Loading extends Component {

    //进度条
    private ProgressBar: ProgressBar = null;

    //进度文字
    private ProgressText: Label = null;

    protected onLoad(): void {

        //判断运行环境
        Global.IsWx = (typeof wx != "undefined");
        Global.IsDy = (typeof tt != "undefined");

        Global.IsDebug && console.log("当前游戏环境", Global.IsWx, Global.IsDy);

        //资源加载初始化
        ResMgr.Instance.LoadInit();

        //广告初始化
        AdMgr.Instance.Init();

        //本地缓存初始化
        LocalStorageMgr.Instance.Init();

        this.ProgressBar = find('ProgressBar', this.node).getComponent(ProgressBar);
        this.ProgressText = find('ProgressText', this.node).getComponent(Label);

        this.LoadRes();
    }

    /**
     * 加载资源
     */
    private async LoadRes() {
        await ResMgr.Instance.LoadBundle(AssetList.BundleName, 0.1);
        await ResMgr.Instance.LoadRes(AssetList.BundleName, AssetList.Asset.SpriteFrames, 0.2);
        await ResMgr.Instance.LoadRes(AssetList.BundleName, AssetList.Asset.Sounds, 0.1);
        await ResMgr.Instance.LoadRes(AssetList.BundleName, AssetList.Asset.Prefabs, 0.6);
        AudioMgr.instance.Init();
        AudioMgr.instance.PlayBgSound(SoundName.Bg);
        await this.destroy();
        await UIMgr.Instance.UdpateScene(AssetList.Scene.LoginScene);
    }

    protected update(dt: number): void {
        this.ProgressBar.progress = ResMgr.Instance.NowProgress;
        this.ProgressText.string = "l" + Math.floor(ResMgr.Instance.NowProgress * 100).toString() + '%';
    }

}


